package com.younion.dao;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.common.base.BaseDAO;
import com.younion.common.exception.BaseException;
import com.younion.common.paginator.Paginator;
import com.younion.vo.*;

 
public class ScoreRuleDAO extends BaseDAO {

	protected static final Log logger = LogFactory.getLog(ScoreRuleDAO.class);

    
    public Collection selectAll() throws BaseException {
        return queryForList("T_Score_Rule.selectAll", null);
    }

    
    public Collection selectByMap(Map paramMap) throws BaseException {
        if(paramMap == null) return null;
        try{
        return queryForList("T_Score_Rule.selectByMap", paramMap);
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

	 
	public int selectCountByMap(Map paramMap) throws BaseException {
		if (paramMap == null)
			return 0;
		try {
			return queryCount("T_Score_Rule.selectByMapCount", paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

     
    	public List selectForPage(Map condition, Paginator paginator) throws BaseException {
		if (condition == null)
			return null;
		int pageIndex = paginator.getStartIndex();
		int perPageSize = paginator.getPageSize();
		String sqlStr = "";
		try {
			Map vo = (Map) condition.get("paramMap");
			if (vo == null) {
				sqlStr = "T_Score_Rule.selectAll";
			} else {
				sqlStr = "T_Score_Rule.selectByMap";
			}
			return queryForPage(sqlStr, vo, pageIndex, perPageSize);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(this.getClass().getSimpleName() +" method error");
		}
	}

    
    public ScoreRuleVO insertByVo(ScoreRuleVO originalVo) throws BaseException {
        if(originalVo == null) return null;
        try{
            return (ScoreRuleVO)this.insert("T_Score_Rule.insertByVo", originalVo);
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

    
    public ScoreRuleVO[] insertVos(ScoreRuleVO[] originalVo) throws BaseException {
        if(originalVo == null) return null;
        try{
        for(int i=0; i<originalVo.length; i++)
        originalVo[i] = insertByVo( originalVo[i]) ;
        return originalVo;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

    
    public Collection insertByList(Collection originalVo) throws BaseException {
        if(originalVo == null) return null;
        try{
        Iterator it = originalVo.iterator();
        while(it.hasNext()) {
        ScoreRuleVO data = (ScoreRuleVO) it.next();
        data = insertByVo(data);
        }
        return originalVo;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

    
    public boolean updateByMap(Map paramMap) throws BaseException {
        if(paramMap == null) return false;
        try{
        this.update("T_Score_Rule.updateByMap", paramMap);
	return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

     
    public boolean updateVos(Map[] originalVo) throws BaseException {
        if(originalVo == null) return false;
        try{
        for(int i=0; i<originalVo.length; i++) {
            if(!updateByMap(originalVo[i])) return false;
        }
        return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

     
    public boolean updateByList(Collection originalVo) throws BaseException {
        if(originalVo == null) return false;
        try{
        Iterator it = originalVo.iterator();
        while(it.hasNext()) {
        Map data = (Map) it.next();
        if(!updateByMap(data)) return false;
        }
        return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

     
    public boolean deleteAll() throws BaseException {
        try{
        return this.getSqlMapClientTemplate().delete("T_Score_Rule.deleteAll", null) > 0?true:false;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

     
    public ScoreRuleVO selectById(Map paramMap) throws BaseException {
        if(paramMap == null || paramMap.isEmpty()) return null;
        try{
        return (ScoreRuleVO)this.getSqlMapClientTemplate().queryForObject("T_Score_Rule.selectById", paramMap);
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }

     
    public boolean deleteById(Map paramMap) throws BaseException {
        if(paramMap == null || paramMap.isEmpty()) return false;
        try{
        this.getSqlMapClientTemplate().delete("T_Score_Rule.deleteById", paramMap);
        return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }
    }
    
    public boolean deleteByIds(List list) throws BaseException {
        if(CollectionUtils.isEmpty(list)) return false;
        try{
        this.getSqlMapClientTemplate().delete("T_Score_Rule.deleteByIds", list);
        return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }    
    }    
     
    public boolean deleteByMap(Map map) throws BaseException {
        if(CollectionUtils.isEmpty(map)) return false;
        try{
        this.getSqlMapClientTemplate().delete("T_Score_Rule.deleteByMap", map);
        return true;
        }catch(Exception e) {
        logger.error(e.getMessage(), e);
        throw new BaseException(this.getClass().getSimpleName() +" method error");
        }           
    }

}